<template>
	<view class="goods-list">
		<view v-for="item in goodsList" :key="item.goods_id" @click="gotoDetail(item.goods_id)">
			<my-goods :goods="item"></my-goods>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				goodsList: [],
				total: 0,
				queryObj: {
					query: '',
					cid: '',
					pagenum: 1,
					pagesize: 10
				}
			}
		},
		onLoad(options){
			this.queryObj = {...this.queryObj,...options}
			this.getGoodsList()
		},
		methods: {
			async getGoodsList(cb){
				if(this.goodsList.length){
					if(this.queryObj.pagenum * this.queryObj.pagesize > this.total) return uni.$showMsg('暂无更多商品')
				}
				const { data } = await uni.$http.get("/goods/search", this.queryObj)
				cb && cb()
				if(data.meta.status !== 200) return uni.$showMsg()
				this.goodsList = [...this.goodsList, ...data.message.goods]
				this.total = data.message.total
			},
			gotoDetail(id){
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?id=' + id
				})
			}
		},
		onReachBottom() {
			this.queryObj.pagenum ++
			this.getGoodsList()
		},
		onPullDownRefresh(e){
			this.goodsList = []
			this.total = 0
			this.queryObj.pagenum = 1
			this.getGoodsList(() => uni.stopPullDownRefresh())
		}
	}
</script>

<style lang="scss"></style>
